/**
 * \class initializeMap
 * \brief This class initialises contents on the map
 * \author Mahlatse Maraba
 */

  
var map;
var markersArray = [];

function initializeMap() {
    if (coordinateFinder.userLat != null && coordinateFinder.userLong !=null)
    {
        var JHBMAP = new google.maps.LatLng(coordinateFinder.userLat,coordinateFinder.userLong);
        var mapOptions = {
            zoom: 14,
            center: JHBMAP,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
    }
    else
    {
        var bussinessMap = new google.maps.LatLng(-26.192291537113462,28.02821529490666);
        var mapOptions = {
            zoom: 14,
            center: bussinessMap,
            mapTypeId: google.maps.MapTypeId.TERRAIN
        };
        map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
    }



    google.maps.event.addListener(map, 'click', function(event) {
        deleteOverlays();
        addMarker(event.latLng);

        //var lat = event.latLng;
        var myeventCoordinates = /\(([-.\d]*), ([-.\d]*)/.exec(  event.latLng );
        if ( myeventCoordinates ) {
            var eventLatitude = parseFloat( myeventCoordinates[1] );
            var eventLongitude = parseFloat( myeventCoordinates[2] );
            eventLatitude  = eventLatitude.toFixed(6);
            eventLongitude = eventLongitude.toFixed(6);
            eventCoordinates.reinitialize(eventLatitude,eventLongitude);


        }
    });
}




/**
 * \brief This function adds marker to the google map.
 */

function addMarker(location) {
    marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markersArray.push(marker);
}


/**
 * \brief This function clears the markers on the map
 */

function clearOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
    }
}


/**
 * \brief This function Shows any overlays currently in the array.
 */
function showOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(map);
        }
    }
}

/**
 * \brief This function deletes all markers in the array by removing references to them.
 */
function deleteOverlays() {
    if (markersArray) {
        for (i in markersArray) {
            markersArray[i].setMap(null);
        }
        markersArray.length = 0;
    }
}
